package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.ActivitiesPublishBean;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.beiing.tianshuai.tianshuai.widget.FooterView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.text.MessageFormat;
import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 活动发布富文本编辑器列表的适配器
 *
 * @author Jinni
 * @date 2017/7/31 0031
 */
public class ActivitiesPublishAdapter extends BaseRecyclerViewAdapter<ActivitiesPublishBean.DataBean.ActiveBean> {

    private static final int DYNAMIC_NORMAL = 0;
    private static final int ITEM_VIEW_TYPE_FOOTER = 1;
    private static final String TAG = "MyOrderAdapter";
    private Context mContext;
    private String mType;
    private int mFooterStatus;
    private OnItemClickListener mOnItemClickListener;

    public void setFooterStatus(int footerStatus) {
        mFooterStatus = footerStatus;
    }

    public ActivitiesPublishAdapter(Context context, String type) {
        this.mContext = context;
        this.mType = type;
    }

    @Override
    public int getItemViewType(int position) {
        int result;
        if (position == mList.size()) {
            result = ITEM_VIEW_TYPE_FOOTER;
        } else {
            result = DYNAMIC_NORMAL;
        }
        return result;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case DYNAMIC_NORMAL:
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_activity_normal, parent, false);
                holder = new ViewHolder(view);
                break;
            case ITEM_VIEW_TYPE_FOOTER:
                if (mFooterView == null) {
                    mFooterView = new FooterView(mContext);
                    mFooterView.setLayoutParams(
                            new LinearLayout.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT
                            )
                    );
                }
                holder = new FooterViewHolder(mFooterView);
                break;
            default:
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            final ActivitiesPublishBean.DataBean.ActiveBean order = mList.get(position);
            switch (mType) {
                case "审核中":
                    ((ViewHolder) holder).mState.setText("审核中");
                    ((ViewHolder) holder).mState.setTextColor(ContextCompat.getColor(mContext, R.color.textColorGrayDark));
                    break;
                case "进行中":
                    ((ViewHolder) holder).mState.setText("已通过");
                    ((ViewHolder) holder).mState.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                    break;
                case "审核失败":
                    ((ViewHolder) holder).mState.setText("未通过");
                    ((ViewHolder) holder).mState.setTextColor(ContextCompat.getColor(mContext, R.color.textColorRed));
                    break;
                case "已结束":
                    ((ViewHolder) holder).mState.setText("已结束");
                    ((ViewHolder) holder).mState.setTextColor(ContextCompat.getColor(mContext, R.color.textColorGrayDark));
                    break;
                default:
                    break;
            }
            RequestOptions coverOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.default_avatar)
                    .centerCrop()
                    .override(360, 200);
            LogUtils.i(order.getCover(), TAG);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(
                            order.getCover()) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.ACTIVITY_PIC_HEAD + order.getCover())
                    .apply(coverOptions)
                    .into(((ViewHolder) holder).mCover);
            ((ViewHolder) holder).mTitle.setText(order.getStrech());
            try {
                ((ViewHolder) holder).mTime.setText(
                        MessageFormat.format(
                                "时间：{0}",
                                TimeUtils.longToString(
                                        Long.parseLong(order.getExitTime()),
                                        "MM.dd HH:mm")
                        )
                );
            } catch (ParseException e) {
                e.printStackTrace();
            }
            ((ViewHolder) holder).mLocation.setText(
                    MessageFormat.format("地点：{0}", order.getLocation())
            );
            ((ViewHolder) holder).itemView.setTag(position);
            ((ViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(view, (Integer) view.getTag());
                    }
                }
            });

        } else if (holder instanceof FooterViewHolder) {
            if (position == mList.size() && mFooterView.getStatus() != FooterView.NO_MORE) {
                setFooterViewStatus(mFooterStatus);
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_activity_state)
        TextView mState;
        @BindView(R.id.iv_activity_cover)
        ImageView mCover;
        @BindView(R.id.tv_activity_title)
        TextView mTitle;
        @BindView(R.id.tv_activity_time)
        TextView mTime;
        @BindView(R.id.tv_activity_location)
        TextView mLocation;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null && mList.size() > 0) {
            return mList.size() + 1;
        } else {
            return 0;
        }
    }

    public interface OnItemClickListener {
        /**
         * 条目点击事件
         *
         * @param view     视图
         * @param position 位置
         */
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}